package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.DeptmentDomain;
import com.xingkeduo.pojo.json.DeptmentJson;
import com.xingkeduo.pojo.query.DeptmentQuery;
import com.xingkeduo.pojo.table.DeptmentTable;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 部门管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/deptment")
public class DeptmentController extends InitController {

    @Autowired
    private DeptmentDomain deptmentDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            DeptmentQuery deptmentQuery = DeptmentJson.createQueryForDeptment(urlParam);
            long id = deptmentQuery.getId();
            DeptmentTable deptmentTable = deptmentDomain.getDeptmentById(id);
            return returnUtil.getSuccess(DeptmentJson.toJson(deptmentTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid DeptmentTable deptmentVo  , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
           // this.errMsg(bindingResult);
            deptmentVo.setCreatedBy(this.getOperator(request));
            log.info("添加部门接口请求参数:{}", JSON.toJSONString(deptmentVo));
            int deptment = deptmentDomain.createDeptment(deptmentVo);
            log.info("添加部门接口返回值:{}", JSON.toJSONString(deptment));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid DeptmentTable deptmentVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            DeptmentTable deptmentTable = DeptmentJson.createTableForModifyDeptment(urlParam,deptmentVo);
            deptmentVo.setUpdatedBy(this.getOperator(request));
            log.info("修改部门接口请求参数:{}", JSON.toJSONString(deptmentTable));
            int i = deptmentDomain.modifyDeptment(deptmentTable);
            log.info("修改部门接口返回值:{}", JSON.toJSONString(i));
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            DeptmentQuery deptmentQuery = DeptmentJson.createQueryForDeptment(urlParam);
            long id = deptmentQuery.getId();
            deptmentDomain.delDeptment(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public String doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            DeptmentQuery deptmentQuery = new DeptmentQuery();

            deptmentQuery=  JSON.parseObject(urlParam,DeptmentQuery.class);

            List<DeptmentTable> deptmentList= deptmentDomain.getDeptmentList(deptmentQuery);

            return returnUtil.getSuccess(DeptmentJson.toJson(deptmentList));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

}
